const db = require("../db")
const lodash = require("lodash")
const date = require("silly-datetime");
const today = date.format(new Date(), 'YYYY-MM-DD HH:mm:ss'); // 2019-01-01
module.exports = {
    getAllChannel:(req,res)=>{
       
        db.query("select channelName,channel_id,channel_pid,channel_level,icon from channel where channel_level = 0;select channelName,channel_id,channel_pid ,channel_level from channel where channel_level != 0",function(err,result){
            if(err) return res.cc(err)
            // let data = lodash.keyBy(result, 'channel_pid')
            // console.log(result);
            let data = {}
            for(let i = 0 ;i < result[0].length;i++){
                
                if(result[0][i].channel_pid == 0){
                    data[result[0][i].channel_id]= {
                        category:result[0][i].channelName,
                        icon:result[0][i].icon,
                        channel_simple:[],
                        channem_detail:[]
                    }
                }
                else if(data.hasOwnProperty(result[0][i].channel_pid)){
                    data[result[0][i].channel_pid].channel_simple.push(result[0][i].channelName)
                    // temp[result[i].channel_pid].channel_simple = lodash.values(lodash.keyBy(result[i],'channelName'))
                }
                
            }
            let temp = {}
            for(let j = 0; j < result[1].length;j++){
                if(result[1][j].channel_level == 1 && !temp.hasOwnProperty(result[1][j].channel_pid)){
                    temp[result[1][j].channel_id] = {
                        main:result[1][j].channelName,
                        pid:result[1][j].channel_pid,
                        goods:[]
    
                    }
                }else if(temp.hasOwnProperty(result[1][j].channel_pid)){
                    temp[result[1][j].channel_pid].goods.push(result[1][j].channelName)
                }
                
            }
            temp = lodash.values(temp)
            temp.forEach(item => {
                data[item.pid].channem_detail.push(item)
                
            });
           res.send({
            code:200,
            msg:"获取列表数据成功！",
            data:lodash.values(data)
           })

        })

    },
    getGoodsList:(req,res)=>{
        let page = req.body.page
        let limit = req.body.limit
        let start = parseInt((page - 1 )*limit)
        let end = page*limit
        db.query('select goods.id as goodId,goods.shopId, goods.goodName ,goods.keyWord,goods.price as priceNow,goods.priceAgo ,goods.saleCount,goods_pic.picName,shopId,shop.shopName from goods,goods_pic,shop where goods.shopId = shop.Id and goods_pic.id in( goods.fileId)  limit ? ,? ',[start,end],function(err,result){
            if(err) return res.cc(err)
            res.send({
                code:200,
                msg:"获取商品列表成功！",
                data:[...result]
            })
        })
    },
    getActivity:(req,res)=>{
        db.query("select activityName as tagContent, activitySource as tagName , activityType as tagType from activities where startTime<= ? and endTime >= ?",[today,today],function(err,result){
            if(err) return res.cc(err)
            return res.send({
                code:200,
                msg:"获取活动列表成功",
                data:[...result]
            })
        })
    },
    
}